<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class article extends core {
	private $article_mod;

	public function __construct() {
		parent::__construct();
		$this->article_mod = m("article");
	}

	//发现首页
	public function index() {
		$where = '';
		$category_id = (isset($_GET['type_id']) && trim($_GET['type_id'])) ? trim($_GET['type_id']) : '';
		//关键词搜索
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		if ($keyword) {
			$where .= " AND (title like '%{$keyword}%' OR content like '%{$keyword}%')";
		}
		if ($category_id) {
			$where .= " AND category_id='{$category_id}'";
		}
		$sql = "SELECT * FROM {$this->tbp}article WHERE status=1 AND is_system=0 {$where} ORDER BY sort ASC, id DESC LIMIT {$offset}, {$pagesize}";
		$articles = $this->db->get_results($sql);
		if ($articles) {
			foreach ($articles as $k=>$article) {
				$articles[$k]->pics = $this->article_mod->_pics($article->id, 3);
				//$articles[$k]->goods = $this->article_mod->_goods($article->id);
				$articles[$k]->add_time = get_time_word($article->add_time);
			}
		}

		$flashes = $this->_flash();
		success(array("flashes" => $flashes, "articles" => $articles));
	}

	//轮播图
	private function _flash() {
		$sql = "SELECT * FROM {$this->tbp}ad WHERE status=1 AND position='faxian' ORDER BY sort ASC, id DESC LIMIT 5";
		//echo $sql;
		$flashes = $this->db->get_results($sql);
		if ($flashes) {
			$flashes = add_domain_deep($flashes, array("pic"));
		}
		return $flashes;
	}

	//添加/修改
	public function add() {
		error_tip('该功能未开通');
		if (IS_POST) {
			$this->save();
		} else {
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			if ($id) {
				$sql = "SELECT * FROM {$this->tbp}article WHERE member_id='{$this->member_id}' AND id='{$id}'";
				$rs = $this->db->get_row($sql);
				success($rs);
			}
			success('ok');
		}
	}

	//保存
	public function save() {
		error_tip('该功能未开通');
		$id = (isset($_POST['id']) && trim($_POST['id'])) ? intval($_POST['id']) : 0;
		$title = isset($_POST['title']) ? trim($_POST['title']) : '';
		$content = isset($_POST['content']) ? trim($_POST['content']) : '';
		$pics = isset($_POST['pics']) ? stripslashes($_POST['pics']) : '';
		$goods = isset($_POST['goods']) ? stripslashes($_POST['goods']) : '';
		if (!$title || !$content || !$pics) error('缺少资料');

		if ($id) {
			$sql = "UPDATE {$this->tbp}article SET title='{$title}', content='{$content}'
				WHERE member_id='{$this->member_id}' AND id='{$id}'";
			$this->db->query($sql);
		} else {
			$sql = "INSERT INTO {$this->tbp}article (title, content, member_id, add_time)
				VALUES ('{$title}', '{$content}', '{$this->member_id}', '".time()."')";
			$this->db->query($sql);
			$id = $this->db->insert_id;
		}

		//保存图片
		if ($pics) {
			$sql = "DELETE FROM {$this->tbp}article_pic WHERE article_id='{$id}'";
			$this->db->query($sql);
			$pics = json_decode($pics);
			foreach ($pics as $p) {
				$sql = "INSERT INTO {$this->tbp}article_pic (article_id, pic, add_time) VALUES ('{$id}', '{$p}', '".time()."')";
				$this->db->query($sql);
			}
		}

		//保存关联产品
		if ($goods) {
			$sql = "DELETE FROM {$this->tbp}article_goods WHERE article_id='{$id}'";
			$this->db->query($sql);
			$goods = json_decode($goods);
			foreach ($goods as $g) {
				$sql = "INSERT INTO {$this->tbp}article_goods (article_id, goods_id) VALUES ('{$id}', '{$g}')";
				$this->db->query($sql);
			}
		}

		$sql = "SELECT * FROM {$this->tbp}article WHERE member_id='{$this->member_id}' AND id='{$id}'";
		$rs = $this->db->get_row($sql);
		success($rs);
	}

	//文章详情
	public function detail() {
		$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
		if ($id<=0) error('缺少参数');
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$row = $this->article_mod->detail($id);
		if (!$row) error('文章不存在');
		$row->content = stripslashes($row->content);
		$row->add_time = date("Y-m-d H:i:s", $row->add_time);
		$row->pics = $this->article_mod->_pics($id); //关联图片
		$row->goods = $this->article_mod->_goods($id); //关联商品
		$row->liked = $this->article_mod->_liked($id); //是否点赞
		$row->likes_list = $this->likes($id);
		$row->comments_list = $this->_comments($id, $offset, $pagesize);
		$sql = "UPDATE {$this->tbp}article SET clicks=clicks+1 WHERE id='{$id}'";
		$this->db->query($sql);
		success($row);
	}

	//文章的点赞
	public function likes($article_id) {
		$sql = "SELECT m.id as member_id, m.avatar, m.nick_name
			FROM {$this->tbp}article_like l LEFT JOIN {$this->tbp}member m ON l.member_id=m.id
			WHERE l.article_id='{$article_id}' ORDER BY l.id DESC";
		$likes = $this->db->get_results($sql);
		return $likes;
	}

	//点赞
	public function like() {
		$article_id = (isset($_POST['article_id']) && trim($_POST['article_id'])) ? intval($_POST['article_id']) : 0;
		$likes = 0;
		if ($this->member_id && $article_id) {
			$sql = "SELECT count(*) FROM {$this->tbp}article_like WHERE article_id='{$article_id}' AND member_id='{$this->member_id}'";
			$exists = intval($this->db->get_var($sql));
			if ($exists) {
				$sql = "DELETE FROM {$this->tbp}article_like WHERE article_id='{$article_id}' AND member_id='{$this->member_id}'";
				$this->db->query($sql);
			} else {
				$sql = "INSERT INTO {$this->tbp}article_like(article_id, member_id, add_time, ip)
					VALUES('{$article_id}', '{$this->member_id}', '".time()."', '".ip()."')";
				$this->db->query($sql);
			}
			$sql = "SELECT count(*) FROM {$this->tbp}article_like WHERE article_id='{$article_id}'";
			$likes = $this->db->get_var($sql);
			$sql = "UPDATE {$this->tbp}article SET likes='{$likes}' WHERE id='{$article_id}'";
			$this->db->query($sql);
		}
		success($likes);
	}

	//发表评论
	public function post_comment() {
		if ($this->member_id<=0) error('请先登录', -100);
		$article_id = (isset($_POST['article_id']) && trim($_POST['article_id'])) ? intval($_POST['article_id']) : 0;
		$content = (isset($_POST['content']) && trim($_POST['content'])) ? trim($_POST['content']) : '';
		$sql = "INSERT INTO {$this->tbp}article_comment(article_id, member_id, content, add_time, ip)
			VALUES('{$article_id}', '{$this->member_id}', '{$content}', '".time()."', '".ip()."')";
		$this->db->query($sql);
		$sql = "UPDATE {$this->tbp}article SET comments=(SELECT count(*) FROM {$this->tbp}article_comment 
			WHERE article_id='{$article_id}') WHERE id='{$article_id}'";
		$this->db->query($sql);
		success($article_id);
	}

	//列出评论
	public function comments() {
		$article_id = (isset($_GET['article_id']) && trim($_GET['article_id'])) ? intval($_GET['article_id']) : 0;
		$offset = (isset($_GET['offset']) && trim($_GET['offset'])) ? intval($_GET['offset']) : 0;
		$pagesize = (isset($_GET['pagesize']) && trim($_GET['pagesize'])) ? intval($_GET['pagesize']) : 8;
		$comments = $this->_comments($article_id, $offset, $pagesize);
		success($comments);
	}

	//列出评论
	public function _comments($article_id, $offset, $pagesize) {
		$sql = "SELECT c.*, m.avatar, m.name as member_name, m.nick_name as member_nick_name
			FROM {$this->tbp}article_comment c LEFT JOIN {$this->tbp}member m ON c.member_id=m.id 
			WHERE article_id='{$article_id}' AND c.parent_id=0 ORDER BY c.id ASC LIMIT {$offset}, {$pagesize}";
		//echo $sql;
		$comments = $this->db->get_results($sql);
		if ($comments) {
			$comments = add_domain_deep($comments, array("avatar"));
			foreach ($comments as $k=>$g) {
				$comments[$k]->add_time = get_time_word($g->add_time);
				$comments[$k]->member_name = get_mark_mobile($g->member_name);
				$sql = "SELECT c.id, c.member_id, c.content, c.add_time, m.name as member_name FROM {$this->tbp}article_comment c 
					LEFT JOIN {$this->tbp}member m ON c.member_id=m.id 
					WHERE c.parent_id='{$g->id}' ORDER BY c.id ASC";
				$replys = $this->db->get_results($sql);
				if ($replys) {
					foreach ($replys as $rk=>$rg) {
						$replys[$rk]->add_time = get_time_word($rg->add_time);
						if ($rg->member_id==0) $replys[$rk]->member_name = '客服';
						$replys[$rk]->member_name = get_mark_mobile($rg->member_name);
					}
				}
				$comments[$k]->replys = $replys;
			}
		}
		return $comments;
	}
	
	//添加评论回复
	public function reply_comment() {
		if ($this->member_id<=0) error('请先登录', -100);
		$article_id = (isset($_POST['article_id']) && intval($_POST['article_id'])) ? intval($_POST['article_id']) : 0;
		$parent_id = (isset($_POST['parent_id']) && intval($_POST['parent_id'])) ? intval($_POST['parent_id']) : 0;
		$content = (isset($_POST['content']) && trim($_POST['content'])) ? trim($_POST['content']) : '';
		if ($article_id<=0 || $parent_id<=0) error('数据错误');
		if (!$content) error('请填写回复内容');
		$time = time();
		$sql = "INSERT INTO {$this->tbp}article_comment (member_id, article_id, parent_id, content, ip, add_time) VALUES 
			('{$this->member_id}', '{$article_id}', '{$parent_id}', '{$content}', '".$this->ip."', '{$time}')";
		$this->db->query($sql);
		$id = $this->db->insert_id;
		success(array('id'=>$id, 'member_name'=>get_mark_mobile($this->member_name), 'content'=>$content, 'add_time'=>get_time_word($time)));
	}

}
